package com.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.course.dto.admin.*;
import com.example.course.mapper.AdminMapper;
import com.example.course.pojo.Admin;
import com.example.course.pojo.Semester;
import com.example.course.service.SemesterService;
import com.example.course.mapper.SemesterMapper;
import com.example.course.utils.common.enums.Code;
import com.example.course.utils.common.result.PageResponseResult;
import com.example.course.utils.common.result.Result;
import com.example.course.utils.config.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 24354
* @description 针对表【semester】的数据库操作Service实现
* @createDate 2024-03-15 20:06:15
*/
@Service
@Slf4j
public class SemesterServiceImpl extends ServiceImpl<SemesterMapper, Semester>
    implements SemesterService{

    @Autowired
    private SemesterMapper semesterMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result saveSemester(AdminSemesterSaveDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //是否重复
        List<Semester> checkSemseterList = semesterMapper.selectList(Wrappers.<Semester>lambdaQuery()
                .eq(Semester::getSemesterMessage, dto.getSemesterMessage()));
        if (!CollectionUtils.isEmpty(checkSemseterList)||checkSemseterList.size()>0) {
            return Result.errorResult(Code.DATA_EXIST);
        }
        //构建学期列表
        Semester semester=new Semester();
        semester.setSemesterStart(dto.getSemesterStart());
        semester.setSemesterEnd(dto.getSemesterEnd());
        semester.setSemesterMessage(dto.getSemesterMessage());
        int checkSave = semesterMapper.insert(semester);
        if (checkSave==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result removeSemester(AdminSemesterRemoveDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //查询删除对象
        Semester checkSemester = semesterMapper.selectById(dto.getSemesterId());
        //删除对象不存在
        if (checkSemester==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        //删除学期
        int checkRemove = semesterMapper.deleteById(dto.getSemesterId());
        //删除失败
        if (checkRemove==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result updateGetSemester(AdminSemesterUpdateGetDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //查询修改对象
        Semester checkSemester = semesterMapper.selectById(dto.getSemesterId());
        //查询为空
        if (checkSemester==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        return Result.okResult(checkSemester);
    }

    @Override
    public Result updateSemester(AdminSemesterUpdateDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //获取修改对象
        Semester checkSemester = semesterMapper.selectById(dto.getSemesterId());
        //构建对象
        Semester semester=new Semester();
        //复制参数
        BeanUtils.copyProperties(checkSemester,semester);
        //不为空时修改对应参数
        if (!dto.getSemesterStart().equals("")) {
            log.info("开始不空");
            semester.setSemesterStart(dto.getSemesterStart());
        }
        if (!dto.getSemesterEnd().equals("")) {
            log.info("结束不空");
            semester.setSemesterEnd(dto.getSemesterEnd());
        }
        if (!dto.getSemesterMessage().equals("")) {
            semester.setSemesterMessage(dto.getSemesterMessage());
        }
        int checkUpdate = semesterMapper.updateById(semester);
        if (checkUpdate==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result getAdminSemester(AdminSemesterGetDto dto) {
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //重置分页
        dto.checkParam();
        //构建分页
        IPage page=new Page(dto.getPage(),dto.getSize());
        //构建json
        LambdaQueryWrapper<Semester> wrapper=new LambdaQueryWrapper<>();
        if (dto.getMemo()!=null) {
            wrapper.like(Semester::getSemesterMessage,dto.getMemo());
        }
        //组件注入
        page=page(page,wrapper);
        //分页json构建
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }
}




